package kevin.workplan.web.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import kevin.lib.util.JsonUtil;
import kevin.workplan.database.dao.CProjectsMapper;
import kevin.workplan.database.gen.model.Plan;
import kevin.workplan.database.gen.model.PlanExample;
import kevin.workplan.database.gen.model.Projects;
import kevin.workplan.exception.BusinessException;
import kevin.workplan.exception.ServiceException;
import kevin.workplan.service.PlanService;
import kevin.workplan.util.Constant;
import kevin.workplan.util.PageUtil;
import kevin.workplan.util.PlanUtil;
import kevin.workplan.util.Util;
import kevin.workplan.web.controller.base.BaseJsonController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/work/plan")
public class PlanController extends BaseJsonController {
	
	@Autowired
	private PlanService planService;
	@Autowired
	private CProjectsMapper projectsMapper ;
	
	@RequestMapping("/list")
	public String list(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception  {
		getProjects(request, session, 1l);
		return "plan/list";
	}
	
	@RequestMapping("/search.json")
	@ResponseBody
	public Map<String,Object> search(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception  {
		
		int pagesize = PageUtil.getPagesize(request);
		int curpage = PageUtil.getCurpage(request);
		
		String orderp = request.getParameter("order");
		String sort = request.getParameter("sort");
		String orderby = null;
		if(Util.empty(orderp) || Util.empty(sort)) {
			 sort = "start_time";	// 默认排序
			 orderp = "asc";
		} else {
			sort = Util.underscoreName(sort);
		}
		orderby = sort + " " + orderp;
		request.setAttribute("orderp", orderp);
		request.setAttribute("sort", sort);
		
		PlanExample px = PlanUtil.genPlanExample(request,1L);
		List<Plan> dataList = planService.search(px ,curpage, pagesize, orderby);
		int count = planService.searchCount(px);
		
		Map<String,Object> data = new HashMap<String,Object>();
		data.put("total", count);
		data.put("rows", dataList);
		
		return successResult(data);
	}
	
	
	
	@RequestMapping("/list2")
	public String list2(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception  {
		
		int pagesize = PageUtil.getPagesize(request);
		int curpage = PageUtil.getCurpage(request);
		
		String orderp = request.getParameter("orderp");
		String sort = request.getParameter("sort");
		String orderby = null;
		if(Util.empty(orderp) || Util.empty(sort)) {
			orderp = "start_time";	// 默认排序
			sort = "asc";
		}
		orderby = orderp + " " + sort;
		request.setAttribute("orderp", orderp);
		request.setAttribute("sort", sort);
		Plan plan = PlanUtil.getPlan(request);
		
		PlanExample px = PlanUtil.genPlanExample(request,1L);
		List<Plan> dataList = planService.search(px, curpage, pagesize, orderby);
		int count = planService.searchCount(px);
		PageUtil.genPage(request,curpage, count,pagesize,dataList);
		
		return "plan/list2";
	}
	
	@RequestMapping("add")
	public String addView(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception {
		Long id = PageUtil.getLong(request, "id");
		
		getProjects(request,session,1l);
		Plan plan = null;
		if(id!=null)
			plan = planService.getById(id);
		else
			plan = new Plan();
		request.setAttribute("plan", plan);
		return "plan/add";
	}
	
	@RequestMapping("save.json")
	@ResponseBody
	public Map<String,Object> save(HttpServletRequest request,
			HttpServletResponse response, HttpSession session)
			throws Exception {
		Plan plan = PlanUtil.getPlan(request);
		
		Projects b = projectsMapper.selectByPrimaryKey(plan.getBid());
		Projects s = projectsMapper.selectByPrimaryKey(plan.getSid());
		
		plan.setBname(b.getName());
		plan.setSname(s.getName());
		
		String idStr = request.getParameter("id");
		if(Util.empty(idStr)) {
			// 新建
			plan.setUid(1l);
			plan.setCreateTime(new Date());
		} else {
			plan.setId(Long.parseLong(idStr));
		}
		if(plan.getProgress()!=null && plan.getProgress().intValue()==Constant.PROGRESS_FINISH) {
			// 完成，记录时间
			plan.setFinishTime(new Date());
		}
		
		if(plan.getId()==null) {
			planService.createPlan(plan);
		} else {
			// TODO 权限判断
			planService.editPlan(plan);
		}
		return successResult(null);
	}
	
	
	private void getProjects(HttpServletRequest request,HttpSession session, Long uid) throws BusinessException, ServiceException {
		List<Projects> bp = planService.getChildProjects(1l, 0l);
		for(Projects p : bp) {
			List<Projects> sp = planService.getChildProjects(1l, p.getId());
			p.setChildren(sp);
		}
		request.setAttribute("bp_json", JsonUtil.toJson(bp));
	}
}